package com.lenovo.controller;

import com.lenovo.pojo.Emp;
import com.lenovo.pojo.Permission;
import com.lenovo.pojo.Result;
import com.lenovo.service.EmpService;
import com.lenovo.service.impl.EmpServiceImpl;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class Menu {
    EmpService service=new EmpServiceImpl();

    /**
     * 显示菜单
     */
    public void showMenu() {
        System.out.println("**************************欢迎进入员工管理系统*************************");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入用密码：");
        String password = scanner.nextLine();
        Result result = login(username, password); //调用登录验证
        Integer code = result.getCode(); //获取登录结果的状态码  401
        int count = 0;

        while (true) {
            if (code == 200) {
                System.out.println("-----------------欢迎"+((Emp)result.getResult()).getName()+"-----------------\n");
                // TODO 显示此用户所对应的功能
                Result result1 = showMenuByEmp(((Emp) result.getResult()).getRole().getId());
                List<Permission> permissionList= (List<Permission>)result1.getResult(); // 拆解返回结果，获取权限list
                for (int i=0;i<permissionList.size();i++){
                    Permission permission = permissionList.get(i);
                    System.out.println((i+1)+"  "+permission.getName());
                }
            } else {
                System.out.println(result.getMsg());
                System.out.println("请输入用户名：");
                username = scanner.nextLine();
                System.out.println("请输入用密码：");
                password = scanner.nextLine();
                Result login = login(username, password);
                code = login.getCode();
                count++;
                if (count == 2) {
                    System.out.println("您已连续三次输入有误，请稍后再试......");
                    System.exit(0);
                }
            }
        }
    }


    /**
     * 登录验证
     * @param account
     * @param password
     * @return
     */
    public Result login(String account,String password){
            Result result=new Result();
        try {
            result = service.login(account, password);
        } catch (SQLException e) {
            result.setCode(403);
            result.setMsg("登录失败");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 显示此用户所对应的功能
     */
    public Result showMenuByEmp(int roleid){
        Result result=new Result();
        try {
            result = service.showMenuByEmp(roleid);

        } catch (SQLException e) {
            result.setCode(401);
            result.setMsg("服务器异常!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            e.printStackTrace();
        }
        return result;
    }
    public void chooseFun(String choose,List<Permission> permissionList){

    }
}

